//***************************************************************
//  File:    GameObject.h
//  Date:    12/17/2012
//  Author:  liangxc (liangxc2010@gmail.com)
//-------------------------------------------------------------
//
//-------------------------------------------------------------
//  This file is part of Hare2D Game Engine.
//  Copyright (C) All Rights Reserved
//***************************************************************
//
//***************************************************************
#include "PCH.h"
#include "Texture.h"
#include "Canvas.h"
#include "RenderSystem.h"

#include "SpriteComponent.h"

namespace hare
{
	HARE_IMPLEMENT_DYNAMIC_CLASS(SpriteComponent, Component, 0)
	{
		HARE_META(zDepth, float)
		HARE_META(offset, PointF)
		HARE_META(hFlip, bool)
		HARE_META(vFlip, bool)
		HARE_META_F(color, uint32, propColor)
		HARE_META(rectUV, RectF)
		//HARE_OBJ_F(mtrl, Material, propImport)
		//HARE_OBJ_F(mtrl, TextureMtrl, propAvoidNull)
		HARE_OBJ_F(mtrl, TextureMtrl, propAllowSubs)
	}

	SpriteComponent::SpriteComponent()
		:zDepth(0)
		,offset(0, 0)
		,hFlip(false)
		,vFlip(false)
		,color(0xffffffff)
		,rectUV(0, 0, 1, 1)
		,mtrl(NULL)
	{
	}

	SpriteComponent::~SpriteComponent()
	{

	}

	void SpriteComponent::render(PointF pos, PointF rotation, PointF scale)
	{
		if (!mtrl)
			return;

		TextureMtrl* textureMtrl = mtrl->getTextureMtrl();
		if (!textureMtrl)
			return;

		Texture* texture = textureMtrl->getTexture();
		if (!texture)
			return;

		RectF r = rectUV;
		RectF drawRect;
		drawRect.minX = 0;
		drawRect.minY = 0;
		drawRect.maxX = (rectUV.maxX - rectUV.minX) * texture->getWidth();
		drawRect.maxY = (rectUV.maxY - rectUV.minY) * texture->getHeight();

		float origoX = offset.x;
		float origoY = offset.y;
		if (hFlip){
			r.minX = rectUV.maxX;
			r.maxX = rectUV.minX;
			//origoX = -origoX;
		}
		if (vFlip){
			r.minY = rectUV.maxY;
			r.maxY = rectUV.minY;
			//origoY = -origoY;
		}

		drawRect.moveTo(pos.x + origoX, pos.y + origoY);

		uint32 c = getCanvas()->getColor();
		float  z = getCanvas()->getZ();
		getCanvas()->setColor(color);
		getCanvas()->setZ(zDepth);
		getCanvas()->drawImage(drawRect, mtrl, r);
		getCanvas()->setColor(c);
		getCanvas()->setZ(z);
	}

	void SpriteComponent::setZ(float z)
	{
		zDepth = z;
	}

	float SpriteComponent::getZ()
	{
		return zDepth;
	}

	void SpriteComponent::setOffset(float x, float y)
	{
		offset.x = x;
		offset.y = y;
	}

	void SpriteComponent::setOffset(const PointF& off)
	{
		offset = off;
	}

	PointF SpriteComponent::getOffset()
	{
		return offset;
	}

	void SpriteComponent::setHFlip(bool flag)
	{
		hFlip = flag;
	}

	bool SpriteComponent::getHFlip()
	{
		return hFlip;
	}

	void SpriteComponent::setVFlip(bool flag)
	{
		vFlip = flag;
	}

	bool SpriteComponent::getVFlip()
	{
		return vFlip;
	}

	void SpriteComponent::setColor(uint32 c)
	{
		color = c;

	}

	uint32 SpriteComponent::getColor()
	{
		return color;
	}

	void SpriteComponent::setUV(float l, float r, float t, float b)
	{
		rectUV.minX = l;
		rectUV.maxX = r;
		rectUV.minY = t;
		rectUV.maxY = b;
	}

	void SpriteComponent::setUV(const RectF& rect)
	{
		rectUV = rect;
	}

	RectF SpriteComponent::getUV()
	{
		return rectUV;
	}

	void SpriteComponent::setMaterial(TextureMtrl::Ptr m)
	{
		mtrl = m;
	}

	TextureMtrl::Ptr SpriteComponent::getMaterial()
	{
		return mtrl;
	}
}